// 菜单管理功能实现
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include "DishManage.h"

// 打印菜单详情 
//  定义了加菜、减菜等函数
void MenuPrint(ListPointer menuPrintPointer)
{
	static int flag = 0;
	struct Menu *menuPointer;
	menuPointer = (struct Menu *)(menuPrintPointer->listData);
	if (flag == 0)
	{
		system("cls");
		printf("\n\n\t\t*************** 菜单信息管理界面 ***************\n\n\n");
		printf("\t\t\t     %-10s%-11s%-12s\n\n", "菜编号", "菜名", "价格");
		flag = 1;
	}
	printf("\t\t\t     %-10s%-11s%-12d\n", menuPointer->dishNo, menuPointer->dishName, 
		menuPointer->dishPrice);
	if (menuPrintPointer->listNext == NULL)
	{
		flag = 0;
	}
}
// 加菜 
void *MenuAdd(void *menuAddData)
{
	char inputDishString[8] = { 0 };
	struct Menu *MenuDataList;
	struct Menu *tempMenuPointer;
	MenuDataList = (struct Menu *)malloc(sizeof(struct Menu));
	tempMenuPointer = (struct Menu *)menuAddData;
	if (menuAddData == NULL)
	{
		system("cls");
		strcpy(MenuDataList->dishNo, "10000");
	}
	else
	{
		FormatInputString(tempMenuPointer->dishNo, '0', '9', 5, 2);
		strcpy(MenuDataList->dishNo, tempMenuPointer->dishNo);
	}
	printf("\n\t\t==> 请输入要增加的菜名：");
	FormatInputString(MenuDataList->dishName, 0, 0, 14, 1);
	printf("\t\t==> 请输入添加菜的价格：");
	MenuDataList->dishPrice = FormatInputString(inputDishString, '0', '9', 2, 2);
	printf("\n");
	return MenuDataList;
}
// 减菜 
int MenuDelete(ListPointer menuDeletePointer)
{
	struct Menu *menu;
	static int dishFlag = 0;
	static char inputDishNo[8] = { 0 };
	if (dishFlag == 0)
	{
		printf("\n\t\t==> 请输入要删除的菜编号(5位)：");
		FormatInputString(inputDishNo, '0', '9', 5, 2);
		dishFlag = 1;
	}
	menu = (struct Menu *)(menuDeletePointer->listData);
	if (strcmp(menu->dishNo, inputDishNo) == 0)
	{
		memset(inputDishNo, 0, sizeof(char));
		dishFlag = 0;
		return 1;
	}
	else
	{
		if (menuDeletePointer->listNext == NULL)
		{
			memset(inputDishNo, 0, sizeof(char));
			dishFlag = 0;
		}
		return 0;
	}
}
// 更新 
int MenuUpdate(ListPointer menuUpdatePointer)
{
	struct Menu *menu;
	static int dishFlag = 0;
	static char inputDishNo[8] = { 0 };
	if (dishFlag == 0)
	{
		printf("\n\t\t==> 请输入要修改的菜编号(5位)：");
		FormatInputString(inputDishNo, '0', '9', 5, 2);
		dishFlag = 1;
	}
	menu = (struct Menu *)(menuUpdatePointer->listData);
	if (strcmp(menu->dishNo, inputDishNo) == 0)
	{
		memset(inputDishNo, 0, sizeof(char));
		printf("\n\t\t==> 重新输入菜名：");
		FormatInputString(menu->dishName, 0, 0, 14, 1);
		printf("\n\t\t==> 重新输入价格：");
		menu->dishPrice = FormatInputString(inputDishNo, '0', '9', 2, 2);
		memset(inputDishNo, 0, sizeof(char));
		dishFlag = 0;
		return 1;
	}
	else
	{
		if (menuUpdatePointer->listNext == NULL)
		{
			memset(inputDishNo, 0, sizeof(char));
			dishFlag = 0;
		}
		return 0;
	}
}
// 查询菜肴 
void MenuSearch(ListPointer menuHeadPointer)
{
	char inputSring[8] = { 0 };
	struct Menu *data;
	ListPointer templistPointer = LinkedListInit(NULL);
	ListPointer listPointer = menuHeadPointer->listNext;
	if (listPointer == NULL)
	{
		system("cls");
		printf("\n\t\t!!! 不存在此菜单信息，请先添加 !!!");
		return;
	}
	system("cls");
	printf("\n\n\t\t*************** 菜单-查询条件选项 ***************\n\n");
	printf("\t\t\t\t    [1] 全部\n\n");
	printf("\t\t\t\t    [2] 编号");
	printf("\n\n\t\t==> 请输入要查询的条件:");
	if (FormatInputString(NULL, '1', '2', 1, 2) == 1)
	{
		LinkedListPrint(menuHeadPointer, MenuPrint);
		return;
	}
	printf("\n\t\t==> 请输入要查询的菜编号(5位)：");
	FormatInputString(inputSring, '0', '9', 5, 2);
	while (listPointer != NULL)
	{
		data = (struct Menu *)(listPointer->listData);
		if (strcmp(data->dishNo, inputSring) == 0)
		{
			LinkedListAdd(templistPointer, data);
			break;
		}
		listPointer = listPointer->listNext;
	}
	if (listPointer != NULL)
	{
		LinkedListPrint(templistPointer, MenuPrint);
	}
	else
	{
		printf("\n\t\t!!! 输入有误，未查询到相关信息 !!!");
	}
}
